package com.ruoyi.yz.controller;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import com.google.zxing.WriterException;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.yz.util.QRCodeUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.yz.domain.YzConference;
import com.ruoyi.yz.service.IYzConferenceService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 会议Controller
 *
 * @author ruoyi
 * @date 2024-06-14
 */
@Controller
@RequestMapping("/conference/conference")
public class YzConferenceController extends BaseController
{
    private String prefix = "conference/conference";

    @Autowired
    private IYzConferenceService yzConferenceService;

    @RequiresPermissions("conference:conference:view")
    @GetMapping()
    public String conference()
    {
        return prefix + "/conference";
    }

    /**
     * 查询会议列表
     */
    @RequiresPermissions("conference:conference:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(YzConference yzConference)
    {
        startPage();
        List<YzConference> list = yzConferenceService.selectYzConferenceList(yzConference);
        return getDataTable(list);
    }

    /**
     * 导出会议列表
     */
    @RequiresPermissions("conference:conference:export")
    @Log(title = "会议", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(YzConference yzConference)
    {
        List<YzConference> list = yzConferenceService.selectYzConferenceList(yzConference);
        ExcelUtil<YzConference> util = new ExcelUtil<YzConference>(YzConference.class);
        return util.exportExcel(list, "会议数据");
    }

    /**
     * 新增会议
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存会议
     */
    @RequiresPermissions("conference:conference:add")
    @Log(title = "会议", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(YzConference yzConference)
    {
        return toAjax(yzConferenceService.insertYzConference(yzConference));
    }

    /**
     * 修改会议
     */
    @RequiresPermissions("conference:conference:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        YzConference yzConference = yzConferenceService.selectYzConferenceById(id);
        mmap.put("yzConference", yzConference);
        return prefix + "/edit";
    }

    /**
     * 修改保存会议
     */
    @RequiresPermissions("conference:conference:edit")
    @Log(title = "会议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(YzConference yzConference)
    {
        return toAjax(yzConferenceService.updateYzConference(yzConference));
    }

    /**
     * 删除会议
     */
    @RequiresPermissions("conference:conference:remove")
    @Log(title = "会议", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(yzConferenceService.deleteYzConferenceByIds(ids));
    }

    /**
     * 生成二维码
     */
    @GetMapping("/qrcode/{id}")
    public void qrcode(@PathVariable("id") Long id, HttpServletRequest request, HttpServletResponse response) throws IOException, WriterException {
        int port = request.getServerPort();
        String url = request.getScheme() + "://" + request.getServerName() + (port == 80 ? "" : ":" + port) +
                            request.getContextPath() + "/conference/member/signup/" + id;
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, id + ".jpg"));
        QRCodeUtil.Encode2Stream(url, 200, 200, response.getOutputStream(), "jpg");
    }
}
